<?php

   class Company_Product extends database_object{
        
        public $id;
        public $company_id;
        public $product_id;
        
        
        protected static $table_name    = "bo_company_product";
        protected static $db_fields     = array("id", "company_id", "product_id");
        
        
        // Method builds a company_product object
       public function build($companyid=0,$productid=0){
           
            if(!self::isregistered($companyid,$productid)){
                    $company_product                = new Company_Product();
                    $company_product->product_id    = $productid; //company_product
                    $company_product->company_id    = $companyid;
                    $company_product->save();
            }
         }
         
         // Method checs if the combination of company and product exists
       public static function isregistered($com=0, $pro=0){
          

            $sql  = "SELECT * FROM bo_company_product ";
            $sql .= "WHERE product_id = {$pro} ";
            $sql .= "AND company_id = {$com} ";
            $sql .= "LIMIT 1";
            $result_array = self::find_by_sql($sql);
                        return !empty($result_array) ? array_shift($result_array) : false;
	} 
        
        
       // Method returns type of sort defined by number $sorttype
        public function sortTypeFind($sorttype){
            if($sorttype ==0){
               return ""; 
            }
            if($sorttype==1){
                return "order by c.name asc";
            }
            if($sorttype==5){
                return "order by c.name desc";
            }
            if($sorttype==2){
                return "order by m.name asc";
            }
            if($sorttype==6){
                return "order by m.name desc";
            }
            if($sorttype==3){
               return "order by Rating asc ";
            }
            if($sorttype==7){
                return "order by Rating desc";
            }
            if($sorttype==4){
                return "order by Views asc";
            }
            if($sorttype==8){
                return "order by Views desc";
            }
        }
        
        //Searches for all combinations from spesific keyword
        public function search($name,$sorttype){
            
            $st          = self::sortTypeFind($sorttype);
            $database   = Database::getInstance();
            $name       = $database->escape_value($name);

            $sql= "SELECT ";
            $sql.= "c.name AS Bedriftsnavn, ";
            $sql.= "m.name AS Produktnavn, ";
            $sql.= "uo.Rating, ";
            $sql.= "ua.Views, ";
            $sql.= "cm.id ";

            $sql.= "FROM bo_company_product cm  ";


            $sql.= "LEFT JOIN bo_company c ON c.id = cm.company_id ";
            $sql.= "LEFT JOIN bo_product m ON m.id = cm.product_id ";


            $sql.= "LEFT JOIN ( ";
            $sql.= "SELECT connection_id, AVG(value) AS Rating ";
            $sql.= "FROM bo_rate ";
            $sql.= "GROUP BY connection_id ";
            $sql.= ") uo ON cm.id = uo.connection_id ";

            $sql.= "LEFT JOIN ( ";
            $sql.= "SELECT company_product_id, COUNT(*) AS Views ";
            $sql.= "FROM bo_motion ";
            $sql.= "GROUP BY company_product_id ";
            $sql.= ") ua ON cm.id = ua.company_product_id ";



            $sql.= " where c.name LIKE '%$name%' or m.name LIKE '%$name%' ";
            $sql.= " {$st} LIMIT 30 ;";
            
            $array      = "";
            $result_set = $database->query($sql);
            
            while ($row = $database->fetch_array($result_set)) {
                    $array[] = $row;
                }
            return $array;
        }
        
        // Method returns the count of views of a spesific CompanyProduct combination
        public static function getViews($id){
           $database    = Database::getInstance();
           $sql         = "SELECT count(id) as views from bo_motion where company_product_id = {$id}";
           $res         =  $database->query($sql);
        
           if($row = $database->fetch_array($res)){

               $tab = (float)$row['views'];
           }
           
           return $tab;
       }
   
       // Gets the amount of views of a specific link between product and company for the last $days days.
       public function getViewsStat($days, $con_id)
       {
           
           $p = $days-1;
           $result = "[";
           for($i = 0; $i<$days; $i++){
           
               $sql = "SELECT count( id ) AS views
                       FROM bo_motion m
                       WHERE datediff( now( ) , m.timestamp ) = {$i} and m.company_product_id ={$con_id}";
                       
               $res = $this->executeSQL($sql);
               
               
               $result .= "[".$p.",".$res[0][0]."], ";
               
               $p--;
               
           }
           
           $result .= "]";
           return $result;
       }
       
     
   }
   $company_product  = new Company_Product();
   
   ?>
